package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.FollowListContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserAttentionBean;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsAttentionAdd;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsAttentionCancel;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsGetFollowList;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 *
 */


@ActivityScope
public class FollowListPresenter extends BasePresenter<FollowListContract.Model, FollowListContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;

    @Inject
    public FollowListPresenter(FollowListContract.Model model, FollowListContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
    }

    public void follow(ReqParamsAttentionAdd reqParamsAttentionAdd) {
        mModel.follow(AppUtil.entityToMap(reqParamsAttentionAdd)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.followSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    public void unFollow(ReqParamsAttentionCancel reqParamsAttentionCancel) {
        mModel.unFollow(AppUtil.entityToMap(reqParamsAttentionCancel)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.unFollowSuccess();
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }

    /**
     * 查询当前用户所关注的用户列表
     *
     * @param reqParams
     */
    public void getFollowUserList4CurrentUserId(ReqParamsGetFollowList reqParams) {
        mModel.getFollowUserList4CurrentUserId(AppUtil.entityToMap(reqParams)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<UserAttentionBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<UserAttentionBean>> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getFollowUserList4UserIdSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.getListFailed();
                        }
                    }
                });
    }

    /**
     * 查询非当前用户所关注的用户列表
     *
     * @param reqParams
     */
    public void getFollowUserList4OtherUserId(ReqParamsGetFollowList reqParams) {
        mModel.getFollowUserList4OtherUserId(AppUtil.entityToMap(reqParams)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<UserAttentionBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<UserAttentionBean>> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getFollowUserList4UserIdSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.getListFailed();
                        }
                    }
                });
    }

    /**
     * 查询当前用户所关注的店铺列表
     *
     * @param reqParams
     */
    public void getFollowStoreList4CurrentUserId(ReqParamsGetFollowList reqParams) {
        mModel.getFollowStoreList4CurrentUserId(AppUtil.entityToMap(reqParams)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<UserAttentionBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<UserAttentionBean>> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getFollowStoreList4UserIdSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.getListFailed();
                        }
                    }
                });
    }

    /**
     * 查询非当前用户所关注的店铺列表
     *
     * @param reqParams
     */
    public void getFollowStoreList4OtherUserId(ReqParamsGetFollowList reqParams) {
        mModel.getFollowStoreList4OtherUserId(AppUtil.entityToMap(reqParams)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<UserAttentionBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<UserAttentionBean>> resultBean) {
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getFollowStoreList4UserIdSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.getListFailed();
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}